package cn.doitedu.javase.socket.demo4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class _01_聊天服务端 {
    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = new ServerSocket(8888);

        Socket socket = serverSocket.accept();

        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));


        try {
            Scanner scanner = new Scanner(System.in);

            String line;
            while ((line = br.readLine()) != null) {
                System.out.println("客户端: " + line);

                // 从控制台获取要回的消息
                String msg = scanner.nextLine();

                // 发送给客户端
                bw.write(msg);
                bw.newLine();
                bw.flush();

            }
        }catch (Exception e){

        }

    }
}
